#include "routeitemwidget.h"
#include "dialog/routeadddialog.h"
#include "ui_routeitemwidget.h"
#include "utils/metric.h"

#include <QMenu>
#include <QMessageBox>

#include <utils/scripts.h>

RouteItemWidget::RouteItemWidget(QWidget *parent) :
    QWidget(parent)
    , ui(new Ui::RouteItemWidget)
    , m_route_modified(false)
    , m_route_deleted(false)
    , m_route_contains_metric(false)
{
    ui->setupUi(this);
}

RouteItemWidget::~RouteItemWidget()
{
    delete ui;
}

RouteItemWidget* RouteItemWidget::make(QString &route)
{
    RouteItemWidget *route_widget = new RouteItemWidget;
    route_widget->ui->label->setText(route.trimmed());

    // no metric
    if (!route.contains("metric")) {
        route_widget->ui->spinBox->hide();
    } else {
        route_widget->m_route_contains_metric = true;
        route_widget->ui->spinBox->setValue(Metric::metric(route));
    }
    route_widget->m_route = route.trimmed();
    route_widget->connectSignals();
    return route_widget;
}

void RouteItemWidget::connectSignals()
{
    connect(ui->spinBox, &QSpinBox::textChanged, this, &RouteItemWidget::slot_spinBox_textChanged);
}

QString RouteItemWidget::scripts()
{
    QStringList script;
    script << script_del();

    if (!m_route_deleted)
        script << script_add();

    return script.join("\n");
}

QString RouteItemWidget::script_add()
{
    auto new_route = m_route;
    if (m_route_contains_metric) {
        auto metric = Metric::find_metric(m_route);
        new_route = new_route.replace(metric, QString::number(ui->spinBox->value()));
    }
    return "sudo ip route add " + new_route;
}

QString RouteItemWidget::script_del()
{
    auto route = m_route;
    if (route.contains("linkdown")) {
        route.remove("linkdown");
    }
    return "sudo ip route del " + route;
}

QString RouteItemWidget::route_now()
{
    auto new_route = m_route;
    if (m_route_contains_metric) {
        auto metric = Metric::find_metric(m_route);
        new_route = new_route.replace(metric, QString::number(ui->spinBox->value()));
    }
    return new_route;
}

QString RouteItemWidget::route() const
{
    return m_route;
}

void RouteItemWidget::setStrikeOut(bool show)
{
    auto font = ui->label->font();
    font.setStrikeOut(show);
    ui->label->setFont(font);
}

void RouteItemWidget::on_RouteItemWidget_customContextMenuRequested(const QPoint &pos)
{
    QMenu menu;
    menu.addAction(ui->action_modify);
    menu.addAction(ui->action_delete);
    menu.addAction(ui->action_restore_delete);
    menu.addAction(ui->action_apply);

    ui->action_modify->setEnabled(!m_route_deleted);
    ui->action_delete->setEnabled(!m_route_deleted);
    ui->action_restore_delete->setEnabled(m_route_deleted);
    ui->action_apply->setEnabled(m_route_modified);
    menu.exec(QCursor::pos());
}

void RouteItemWidget::on_action_delete_triggered()
{
    setStrikeOut(m_route_deleted = true);
    emit onDelete(this);
}

void RouteItemWidget::on_action_restore_delete_triggered()
{
    setStrikeOut(m_route_deleted = false);
    emit onRestoreDelete(this);
}


void RouteItemWidget::on_action_modify_triggered()
{
    RouteAddDialog dialog;
    dialog.setRoute(m_route);
    if (dialog.exec() == QDialog::Accepted) {
        Scripts::bash_scripts("pkexec ip route add " + dialog.route());
        m_route= dialog.route();
    }
}

void RouteItemWidget::on_action_apply_triggered()
{
    auto answer = QMessageBox::question(this, "确定应用?", "将使用新的路由:\n" + route_now());
    if (answer == QMessageBox::Yes) {
        emit applySignal(this);
        m_route_modified = false;
    }
}

void RouteItemWidget::slot_spinBox_textChanged(QString arg1)
{
    m_route_modified = true;
}
